package com.mind.api.serivce;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mind.common.exception.ServiceException;
import com.mind.common.utils.StringUtils;
import com.mind.system.domain.SysConfig;
import com.mind.system.mapper.SysConfigMapper;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Log4j2
public class ShareServiceImpl {
    private String baseUrl = "http://172.20.190.194:31417";

//    @Resource
//    private AjTicketTypeMapper ticketTypeMapper;

    @Resource
    private SysConfigMapper sysConfigMapper;

    // 加载配置
    private void initBase(){
        SysConfig sysConfig = sysConfigMapper.checkConfigKeyUnique("SYNC_BASEURL");
        if(sysConfig != null && StringUtils.isNotEmpty(sysConfig.getConfigValue())){
            baseUrl = sysConfig.getConfigValue();
        }
    }
    // 获取单据类型
    public JSONArray getTicketType(){
        initBase();
        try {
            String url = baseUrl + "/syncDocnumTypeByPageRest_PS?_wadl";
            String params = "{\"Request\":{\"Head\":{\"ACCOUNT\":\"ZNSD\",\"BIZTRANSACTIONID\":\"ZNSD_XXXXXX01\",\"COMMENTS\":\"\",\"COMPANY\":\"\",\"CONSUMER\":\"\",\"COUNT\":\"\",\"PASSWORD\":\"\",\"SRVLEVEL\":\"\",\"USE\":\"\"},\"Data\":{\"pageNum\":\"1\",\"pageSize\":\"9999\"}}}";
            OkHttpClient client = new OkHttpClient();

            RequestBody body = RequestBody.create(params, MediaType.get("application/json; charset=utf-8"));

            // 创建请求

            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();

            Response response = client.newCall(request).execute();
            String bodyStr = response.body().string();
            log.info("请求地址：{}, 请求参数： {}, 请求响应:{}", request.url(), params, bodyStr);
            // 获取响应
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            // 打印响应体
            JSONObject result = JSON.parseObject(bodyStr);
            JSONObject resp = result.getJSONObject("Response");
            JSONArray data = resp.getJSONObject("Data").getJSONObject("data").getJSONArray("records");
            return data;
        }catch (Exception e){
            log.error("同步单据类型报错：", e);
            throw new ServiceException("同步出错");
        }
    }

    // 获取业务大小类
    public JSONArray getBusinessCate(){
        try {
            initBase();
            String url = baseUrl + "/syncSubBizByPageRest_PS?_wadl";
            String params = "{\"Request\":{\"Head\":{\"ACCOUNT\":\"ZNSD\",\"BIZTRANSACTIONID\":\"ZNSD_XXXXXX01\",\"COMMENTS\":\"\",\"COMPANY\":\"\",\"CONSUMER\":\"\",\"COUNT\":\"\",\"PASSWORD\":\"\",\"SRVLEVEL\":\"\",\"USE\":\"\"},\"Data\":{\"pageNum\":\"1\",\"pageSize\":\"9999\", \"startDate\":\"1949-04-01 00:00:00\"}}}";
            OkHttpClient client = new OkHttpClient();

            RequestBody body = RequestBody.create(params, MediaType.get("application/json; charset=utf-8"));

            // 创建请求

            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();

            Response response = client.newCall(request).execute();
            String bodyStr = response.body().string();
            log.info("请求地址：{}, 请求参数： {}, 请求响应:{}", request.url(), params, bodyStr);
            // 获取响应
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            // 打印响应体
            JSONObject result = JSON.parseObject(bodyStr);
            JSONObject resp = result.getJSONObject("Response");
            JSONArray data = resp.getJSONObject("Data").getJSONObject("data").getJSONArray("records");
            return data;
        }catch (Exception e){
            log.error("同步单据类型报错：", e);
            throw new ServiceException("同步出错");
        }
    }
}
